// 封装购物车模块
import { ref } from 'vue'
import { defineStore } from 'pinia'
import { computed } from 'vue'
import { useUserStore } from './user'
import { insertCartAPI,findNewCartListAPI,delCartAPI} from '@/apis/cart'

export const useCartStore = defineStore('cart', () => {
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    
    const cartList = ref([])
    const addCart =async (goods) => {
        const { skuId, count } = goods
        if (isLogin.value) { 
            await insertCartAPI({ skuId, count })
            const res = await findNewCartListAPI()
            cartList.value = res.result
        }
        else {
        // 添加商品到购物车的逻辑
        //已经添加过 count+1
        //没添加过 直接push
        const item = cartList.value.find((item) => goods.skuId === item.skuId)
        if (item) {
            //找到了
            item.count++
        } else {
            //没找到
            cartList.value.push(goods)
        }
        }
    }

    //获取最新购物车列表
    const updateCartList = async () => {
        const res = await findNewCartListAPI()
        cartList.value = res.result
    }

    
    //删除购物车
    const delCart = async (skuId) => {
        if (isLogin.value) {
            await delCartAPI([skuId])
             const res = await findNewCartListAPI()
            cartList.value = res.result
        } else {
         //删除商品
        const index = cartList.value.findIndex(item => item.skuId === skuId)
        if (index !== -1) {
            cartList.value.splice(index, 1)    
        }
        }
    }

    //清除购物车
    const clearCart = () => {
        cartList.value = []
    }

    //计算属性
    //1.总数 2.总价 3.已选择数量 4.已选择商品价格合计
    const allCount = computed(() => 
       cartList.value.reduce((preValue, item) => preValue + item.count, 0) //preValue是上次的结果 item是当前项
    )
       const allPrice = computed(() => 
       cartList.value.reduce((preValue, item) => preValue + item.count*item.price, 0)
    )
    const selectedCount = computed(() => 
       cartList.value.filter(item => item.selected).reduce((preValue, item) => preValue + item.count, 0)
    ) 
        const selectedPrice = computed(() => 
       cartList.value.filter(item => item.selected).reduce((preValue, item) => preValue + item.count*item.price, 0)
    ) 



    //单选功能
    const singleCheck = (skuId, selected) => {
        //找到对应的商品
        const item = cartList.value.find((item) => item.skuId === skuId)
            item.selected = selected
    }
    //是否全选
    const isAll = computed(() => 
       cartList.value.every(item => item.selected) 
    )
    //全选功能
    const allCheck = (selected) => {
        //循环每个商品
        cartList.value.forEach(item => item.selected = selected)
    }
    
    //

    
    return {
        cartList,
        addCart,
        delCart,
        allCount,
        allPrice,
        singleCheck,
        isAll,
        allCheck,
        selectedCount,
        selectedPrice,
        clearCart,
        updateCartList
        }
}, {
    persist: true
}
)   